package com.zzw.controller;


import com.zzw.bean.ClassDetailsVo;

import com.zzw.bean.OrderFormVo;
import org.apache.log4j.LogManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzw.service.impl.OrderFormServiceImpl;
import com.zzw.utils.Result;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by 张志伟 on 2017/11/20.
 */

@Controller
@RequestMapping("/orderform")
public class OrderFormController {

    @Resource
    private OrderFormServiceImpl orderformService;
    private static org.apache.log4j.Logger logger = LogManager.getLogger(UserController.class);
    /**
     * desc：通过用户ID获取所有订单（已支付 - 我的订单）
     *
     */
    @ResponseBody
    @RequestMapping("getOrderFormByUid")
    Result<ArrayList<OrderFormVo>> getOrderFormByUid(Integer uid) {
        ArrayList<OrderFormVo> ofList = orderformService.getOrderFormByUid(uid);
        Result<ArrayList<OrderFormVo>> stringResult = new Result<ArrayList<OrderFormVo>>();

        stringResult.setCode(0);
        stringResult.setMsg("查询成功");

        stringResult.setData(ofList);
        return stringResult;
    }

//    /**
//     * desc：通过用户ID获取课程表（已支付 - 我的订单）
//     *
//     */
//    @ResponseBody
//    @RequestMapping("getClassTableByUid")
//    Result<ArrayList<ClassDetailsVo>> getClassTableByUid(Integer uid) {
//        ArrayList<ClassDetailsVo> ofList = orderformService.getOrderFormByUid(uid);
//        Result<ArrayList<ClassDetailsVo>> stringResult = new Result<ArrayList<ClassDetailsVo>>();
//
//        stringResult.setCode(0);
//        stringResult.setMsg("查询成功");
//
//        stringResult.setData(ofList);
//        return stringResult;
//    }

    /**
     * desc：通过用户ID获取该用户购物车内的订单（未支付 - 购物车车）
     *
     */
    @ResponseBody
    @RequestMapping("getShoppingCartByUid")
    Result<ArrayList<OrderFormVo>> getShoppingCartByUid(Integer uid) {
        ArrayList<OrderFormVo> ofList = orderformService.getShoppingCartByUid(uid);
        Result<ArrayList<OrderFormVo>> stringResult = new Result<ArrayList<OrderFormVo>>();

        stringResult.setCode(0);
        stringResult.setMsg("查询成功");

        stringResult.setData(ofList);
        return stringResult;
    }

    /**
     * desc：通过订单ID支付订单
     */
    @ResponseBody
    @RequestMapping("payOrderFormByOid")
    Result<String> payOrderFormByOid(Integer oid) {

        int modifyResult = orderformService.payOrderFormByOid(oid);
        Result<String> stringResult = new Result<String>();

        if (modifyResult == 1) {
            stringResult.setCode(105);
            stringResult.setData("失败");
            stringResult.setMsg("支付订单失败");
        } else if (modifyResult == 0) {
            stringResult.setCode(0);
            stringResult.setData("成功");
            stringResult.setMsg("支付订单成功");
        }

        return stringResult;
    }


    /**
     * desc：通过订单ID数组批量支付订单
     */
    @ResponseBody
    @RequestMapping("payOrderFormByOidList")
    Result<String> payOrderFormByOidList(@RequestBody ArrayList<Integer> OidList) {
       // logger.info(OidList.toString());
        int modifyResult = orderformService.payOrderFormByOidList(OidList);
        Result<String> stringResult = new Result<String>();
       // logger.info(modifyResult);
        if (modifyResult == 1) {
            stringResult.setCode(105);
            stringResult.setData("失败");
            stringResult.setMsg("支付订单失败");
        } else if (modifyResult == 0) {
            stringResult.setCode(0);
            stringResult.setData("成功");
            stringResult.setMsg("支付订单成功");
        }
        return stringResult;
    }


    /**
     * desc：通过订单ID数组批量删除订单
     */
    @ResponseBody
    @RequestMapping("deleteOrderFormByOidList")
    Result<String> deleteOrderFormByOidList(@RequestBody ArrayList<Integer> OidList) {
        // logger.info(OidList.toString());
        int modifyResult = orderformService.deleteOrderFormByOidList(OidList);
        Result<String> stringResult = new Result<String>();
        // logger.info(modifyResult);
        if (modifyResult == 1) {
            stringResult.setCode(106);
            stringResult.setData("失败");
            stringResult.setMsg("删除订单失败");
        } else if (modifyResult == 0) {
            stringResult.setCode(0);
            stringResult.setData("成功");
            stringResult.setMsg("删除订单成功");
        }
        return stringResult;
    }



    /**
     * desc：通过订单ID删除订单
     */
    @ResponseBody
    @RequestMapping("deleteOrderFormByOid")
    Result<String> deleteOrderFormByOid(Integer oid) {

        int modifyResult = orderformService.deleteOrderFormByOid(oid);
        Result<String> stringResult = new Result<String>();

        if (modifyResult == 1) {
            stringResult.setCode(106);
            stringResult.setData("失败");
            stringResult.setMsg("删除订单失败");
        } else if (modifyResult == 0) {
            stringResult.setCode(0);
            stringResult.setData("成功");
            stringResult.setMsg("删除订单成功");
        }

        return stringResult;
    }


    /**
     * desc：通过用户ID支付购物车内所有订单
     */
    @ResponseBody
    @RequestMapping("payShoppingCartByUid")
    Result<String> payShoppingCartByUid(Integer uid) {

        int modifyResult = orderformService.payShoppingCartByUid(uid);
        Result<String> stringResult = new Result<String>();

        if (modifyResult == 1) {
            stringResult.setCode(107);
            stringResult.setData("失败");
            stringResult.setMsg("支付购物车失败");
        } else if (modifyResult == 0) {
            stringResult.setCode(0);
            stringResult.setData("成功");
            stringResult.setMsg("支付购物车成功");
        }

        return stringResult;
    }

    /**
     * desc：通过用户ID和课程ID新增一个订单（支付/未支付 - 1/0）
     */
    @ResponseBody
    @RequestMapping("addOrderFormByCidAndUid")
    Result<String> addOrderFormByCidAndUid(Integer cid, Integer uid, Integer ispay) {

        logger.info("是否支付"+ispay);
        int modifyResult = orderformService.addOrderFormByOrderForm(cid, uid, ispay);
        Result<String> stringResult = new Result<String>();

        if (modifyResult == 1) {
            stringResult.setCode(108);
            stringResult.setData("失败");
            stringResult.setMsg("添加失败");
        } else if (modifyResult == 0) {
            stringResult.setCode(0);
            stringResult.setData("成功");
            stringResult.setMsg("添加成功");
        }

        return stringResult;
    }

}
